package com.tm.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.login.result.Result;
import com.tm.media.model.entity.StatisticsEntity;
import com.tm.media.model.entity.WmFansStatisticsEntity;
import com.tm.media.model.mapper.NewsMapper;
import com.tm.media.model.mapper.StatisticsMapper;
import com.tm.media.model.mapper.WmFansStatisticsMapper;
import com.tm.media.model.vo.NewVo;
import com.tm.media.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @Author Ly
 * @Data 2023/2/25 17:06
 * @ClassName StatisticsServiceImpl
 */
@Service
public class StatisticsServiceImpl extends ServiceImpl<StatisticsMapper, StatisticsEntity> implements StatisticsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private WmFansStatisticsMapper wmFansStatisticsMapper;
    @Override
    public Result news(NewVo newVo) {
        List<StatisticsEntity> newsEntities=null;
        if (newVo.getType()==1){
             newsEntities= newsMapper.benzhou();
        }else if (newVo.getType()==0){
             newsEntities= newsMapper.jintian();
        }else if (newVo.getType()==7){
            newsEntities= newsMapper.jinqitian();
        }else if (newVo.getType()==30){
            newsEntities = newsMapper.oneMonth();
        }else {
            newsEntities = newsMapper.guding(newVo);
        }
        return Result.SUCCESS(newsEntities);
    }

    @Override
    public Result fans(NewVo newVo) {
        Date date1 = new Date();
        Date date2= new Date();
        if (newVo.getStime()!=null){
            String startcurrentTime = new SimpleDateFormat("yyyy-MM-dd").format(newVo.getStime());
            String endcurrentTime = new SimpleDateFormat("yyyy-MM-dd").format(newVo.getEtime());
            try {
                date1 = new SimpleDateFormat("yyyy-MM-dd").parse(startcurrentTime);
                date2 = new SimpleDateFormat("yyyy-MM-dd").parse(endcurrentTime);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        QueryWrapper<WmFansStatisticsEntity> queryWrapper = new QueryWrapper<>();
        if (newVo.getType()==0){
            queryWrapper.eq("created_time",LocalDate.now());
        }else if (newVo.getType()==1){
            queryWrapper.between("created_time",date1,date2);
        }else if (newVo.getType()==7){
            queryWrapper.between("created_time", date2,date1);
        }else if (newVo.getType()==30){
            queryWrapper.between("created_time", date2,date1);
        }else {
            queryWrapper.between("created_time", date1,date2);
        }
        List<WmFansStatisticsEntity> wmFansStatisticsEntities = wmFansStatisticsMapper.selectList(queryWrapper);
        return Result.SUCCESS(wmFansStatisticsEntities);
    }
}
